package logic

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/google/uuid"
	"models"
	"pkg"
	"time"

	"vip_srv/internal/svc"
	"vip_srv/vip_srv"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderPushLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewOrderPushLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderPushLogic {
	return &OrderPushLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *OrderPushLogic) OrderPush(in *vip_srv.OrderPushRequest) (*vip_srv.OrderPushResponse, error) {
	// todo: add your logic here and delete this line
	var user models.User
	if err := models.DB.Where("id = ?", in.UserId).Limit(1).Find(&user).Error; err != nil {
		return nil, err
	}
	if user.ID == 0 {
		return nil, fmt.Errorf("账号不可用")
	}
	if user.Status != 0 {
		return nil, fmt.Errorf("账号状态异常")
	}
	var order = models.Order{
		UserId:   in.UserId,
		AskMonth: in.AskMonth,
		Status:   "未支付",
		Snow:     uuid.NewString(),
		Total:    pkg.VipTotal(in.AskMonth),
	}
	set, _ := json.Marshal(&order)
	models.RDB.Set(models.Ctx, order.Snow, set, 30*time.Minute)
	return &vip_srv.OrderPushResponse{
		Snow: order.Snow,
	}, nil
}
